package com.orange.eduservice.controller;


import com.orange.commonutils.result.Result;
import com.orange.eduservice.entity.EduChapter;
import com.orange.eduservice.entity.vo.ChapterVo;
import com.orange.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author orange
 * @since 2021-12-17
 */
@RestController
@CrossOrigin
@Api(tags = "章节管理")
@RequestMapping("/eduService/edu-chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    //获取课程大纲列表，根据课程id进行查询
    @ApiOperation("根据课程id获取大纲列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(@ApiParam("课程id") @PathVariable String courseId) {

        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("allChapterVideo", list);
    }

    //添加章节
    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public Result addChapter(@ApiParam("章节对象") @RequestBody EduChapter eduChapter) {

        eduChapterService.save(eduChapter);
        return Result.ok();
    }

    //根据章节id查询
    @ApiOperation("根据章节id查询")
    @GetMapping("/getChapter/{chapterId}")
    public Result getChapter(@ApiParam("章节id") @PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return Result.ok().data("chapter", eduChapter);
    }

    //修改章节
    @ApiOperation("修改章节")
    @PostMapping("/updateChapter")
    public Result updateChapter(@ApiParam("章节对象") @RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    //根据id删除章节
    @ApiOperation("根据id删除章节")
    @DeleteMapping("/deleteById/{chapterId}")
    public Result deleteById(@ApiParam("章节id") @PathVariable String chapterId) {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

